const dataBase = require('../dataBase')

// 上传墙信息
function addConfessionWall(req, res){
    const confessionWallInfo = req.body

    const addConfessionWallSql = 'insert into confessionwall(wallCopy, releaseTime, uId, confessionWallImgs, selectContactInfoFlag) VALUE (?, ?, ?, ?, ?)'

    dataBase.query('SET NAMES utf8mb4', (err, results) => {
        if(err){
            return res.ErrorFun('上传失败！')
       }
    })

    dataBase.query(addConfessionWallSql, [confessionWallInfo.wallCopy, confessionWallInfo.releaseTime, confessionWallInfo.uId, confessionWallInfo.confessionWallImgs, confessionWallInfo.selectContactInfoFlag], (err, results) => {
        if(err){
            return res.ErrorFun("未知错误！请稍后重试！")
        }

        if(results.affectedRows !== 1){
            return res.ErrorFun('上传失败！')
        }

        res.send({
            status: 200,
            message: "上传成功!"
        })
    })   
}

// 分页（懒加载）墙数据
function getConfessionWallInfoList(req, res){
    const getConfessionWallInfoListSql = 'select * from confessionwall order by releaseTime DESC'

    dataBase.query('SET NAMES utf8mb4', (err, results) => {
        if(err){
            return res.ErrorFun('上传失败！')
       }
    })

    dataBase.query(getConfessionWallInfoListSql, (err, results) => {
        if(err){
            return res.ErrorFun('未知错误！请稍后重试！')
        }

        res.send({
            status: 200,
            message: '获取成功！',
            confessionWallInfoList: results
        })
    })
}

// 获取墙上传者的基本信息
function getPublisherInfo(req, res){

    const uId = req.query.uId
    const selectContactInfoFlag = req.query.selectContactInfoFlag

    const getPublisherInfo_haveQQ_Sql = 'select headImageSrc, sex, nickname, QQMail, university from personalinformation where id = ?'
    const getPublisherInfo_Sql = 'select headImageSrc, nickname, university from personalinformation where id = ?'

    if(selectContactInfoFlag === "0"){

        dataBase.query(getPublisherInfo_Sql, uId, (err, results) => {
            if(err){
                return res.ErrorFun('未知错误！请稍后重试！')
            }

            res.send({
                status: 200,
                message: '获取成功！',
                publisherInfo: results[0]
            })
        })

    }else{
        dataBase.query(getPublisherInfo_haveQQ_Sql, uId, (err, results) => {
            if(err){
                return res.ErrorFun('未知错误！请稍后重试！')
            }

            res.send({
                status: 200,
                message: '获取成功！',
                publisherInfo: results[0]
            })
        })

    }

}

module.exports = {
    addConfessionWall,
    getConfessionWallInfoList,
    getPublisherInfo
}